
'''	
	This file is part of Arendal.
	
	Copyright (C) 2008	Mohamed Hazem <mohamed.hazem@student.guc.edu.eg>

	
	Arendal is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Arendal is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Arendal.	If not, see <http://www.gnu.org/licenses/>.
'''


from PyQt4.QtCore import QVariant, Qt, SIGNAL, QString, QStringList
from PyQt4.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
from PyQt4.QtGui import QWidget, QApplication, QLineEdit, QHBoxLayout, QVBoxLayout, QLabel, QPushButton, QMessageBox, QGridLayout, QCompleter
from registrationForm import registrationForm
from userView import userView
from adminView import adminView
import sys


db = QSqlDatabase.addDatabase("QMYSQL")
db.setDatabaseName("AgendaDB")
db.setHostName("localhost")
db.setUserName("root")
db.setPassword("hellothere")


class loginWindow(QWidget):
	def __init__(self):
		app = QApplication(sys.argv)
		app.setEffectEnabled(Qt.UI_AnimateTooltip)

		QWidget.__init__(self)
		self.setWindowTitle("Please Login")
		self.setFixedSize(200, 150)

		self.usernameLabel = QLabel("Username:")
		self.usernameLineEdit = QLineEdit()
		self.usernameLabel.setToolTip("Teeeeeee")

		self.passwordLabel = QLabel("Password:")
		self.passwordLineEdit = QLineEdit()
		self.passwordLineEdit.setEchoMode(QLineEdit.Password)

		self.loginButton = QPushButton("Login")
		self.connect(self.loginButton, SIGNAL("clicked()"), self.loginButtonClicked)

		self.registerLabel = registerLabel()

		grid = QGridLayout()

		grid.addWidget(self.usernameLabel, 1, 0)
		grid.addWidget(self.usernameLineEdit, 1, 1)

		grid.addWidget(self.passwordLabel, 2, 0)
		grid.addWidget(self.passwordLineEdit, 2, 1)

		grid.addWidget(self.loginButton, 3, 1)

		grid.addWidget(self.registerLabel, 4, 1)

		self.setLayout(grid)
		
		self.show()
		app.setStyleSheet("QLineEdit {background: white; border: 2px solid gray; border-radius: 10px; padding: 0 8px; selection-background-color: darkgray;}")
		sys.exit(app.exec_())

	def loginButtonClicked(self):
		if self.usernameLineEdit.text() == '' or self.passwordLineEdit.text() == '':
			QMessageBox.warning(None, "Warning", "Please fill in the required fields")
			self.usernameLineEdit.clear()
			self.passwordLineEdit.clear()
			return
		else:
			if not db.open():
				QMessageBox.warning(None, "Warning", "Database Error: %s" % (db.lastError().text()))
				self.usernameLineEdit.clear()
				self.passwordLineEdit.clear()
				return

			self.modelUser = QSqlTableModel(self)
			self.modelUser.setTable("User")
			self.modelUser.setFilter("binary username='%s' AND binary password='%s'" % (self.usernameLineEdit.text(), self.passwordLineEdit.text()))
			self.modelUser.select()
			self.modelUser.query().lastQuery()

			if self.modelUser.rowCount() == 0:
				QMessageBox.warning(None, "Warning", "Wrong Username or Password")
			elif self.modelUser.record(0).value("type").toString() == 'User':
				self.userViewWindow = userView(self.modelUser.record(0).value("userID").toInt()[0])
			else:
				self.adminViewWindow = adminView(self.modelUser.record(0).value("userID"))
			
			self.usernameLineEdit.clear()
			self.passwordLineEdit.clear()

class registerLabel(QLabel):
	def __init__(self):
		QLabel.__init__(self, "<b><u><font color=blue>Register</font></u></b>")

		self.setCursor(Qt.PointingHandCursor)

	def mousePressEvent(self, event):
		if not db.open():
			QMessageBox.warning(None, "Warning", "Database Error: %s" % (db.lastError().text()))
		else:
			self.registrationFormWindow = registrationForm()


boo = loginWindow()
